<template>
    <div class="management">
        <div class="header">
          <h2>会议室管理</h2>
          <div class="role-buttons">
            <el-button type="primary" size="small" @click="handleNew">
              <el-icon><Plus /></el-icon>新增会议室
            </el-button>
          </div>
        </div>
      </div>

      <!-- 会议室表格 -->
      <div class="content">
      <el-table :data="roomList" class="room-table">
        <el-table-column prop="name" label="会议室名称"/>
        <el-table-column prop="type" label="类型">
          <template #default="{ row }">
            <el-tag :type="row.type === 'classroom' ? 'success' : 'warning'">
              {{ row.type === 'classroom' ? '教室型' : '圆桌型' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="capacity" label="座位数"/>
        <el-table-column prop="hourlyRate" label="价格(元/小时)"/>
        <el-table-column prop="area" label="面积(㎡)"/>
        <el-table-column prop="equipments" label="设备">
          <template #default="{ row }">
            <el-tag v-for="equipment in row.equipments" :key="equipment" type="primary">
              {{ equipmentMap[equipment] || equipment }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button 
              type="primary" 
              size="small"
              @click="handleEdit(row)"
            >
              编辑
            </el-button>
            <el-popconfirm 
              title="确定删除该会议室？"
              @confirm="handleDelete(row.id)"
            >
              <template #reference>
                <el-button 
                  type="danger" 
                  size="small"
                >
                  删除
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>

      <!-- 新增/编辑对话框 -->
      <MeetingRoomForm 
        v-model:visible="showCreateDialog"
        :form-data="currentRoom"
        @submit="handleFormSubmit"
      />
    </div>
  </template>

  <script setup>
  import { ref, reactive, computed } from 'vue'
  import { ElMessage } from 'element-plus'
  import MeetingRoomForm from '../../components/MeetingRoomForm.vue'
  import { getMeetingRooms, createMeetingRoom, updateMeetingRoom, deleteMeetingRoom} from '../../utils/admin/meetingRoom.js'
  

  const equipmentMap = {
    projector: '投影仪',
    audio: '音响系统',
    network: '高速网络',
    whiteboard: '白板',
  }
  
  const roomList = ref([]) // 实际应从API获取
  const showCreateDialog = ref(false)
  const currentRoom = reactive({
    id: null,
    name: '',
    type: 'classroom',
    capacity: 100,
    hourlyRate: 60,
    area: 50,
    equipments: [],
  })
  
  
  const handleEdit = (row) => {
    Object.assign(currentRoom, row)
    showCreateDialog.value = true
  }
  
  // 新增会议室
  const handleNew = () => {
    Object.assign(currentRoom, {
      id: null,
      name: '',
      type: 'classroom',
      capacity: 100,
      hourlyRate: 60,
      area: 50,
      equipments: [],
    })
    showCreateDialog.value = true
  }
  
  const handleFormSubmit = async (form) => {
    try {
      // 实际应调用API
      if (form.id) {
        // 更新逻辑
        await updateMeetingRoom(form).then(res=>{
          console.log(res)
        })
      } else {
        // 新建逻辑
        await createMeetingRoom(form).then(res=>{
          console.log(res)
        })
      }
      showCreateDialog.value = false
      ElMessage.success('操作成功')
      await fetchRoomList()
    } catch (error) {
      ElMessage.error(error.message)
    } finally{
      await fetchRoomList()
    }
  }

  const handleDelete = async (id) => {
    try {
      await deleteMeetingRoom(id).then(res=>{
        console.log(res)
      })
    }catch (error) {
      ElMessage.error(error.message)
    } finally{
      console.log('fetchRoomList');
      await fetchRoomList()
    }
  }
  
  const fetchRoomList = async () => {
    try {
      const response = await getMeetingRooms();
      if (response.data.code === 200) {
        roomList.value = response.data.data
      }
    } catch (error) {
      ElMessage.error('获取会议室列表失败');
    }
  }
  
  // 在组件挂载时调用
  fetchRoomList()
  </script>

  <style scoped>
  .header {
    background: white;
    padding: 24px;
    border-radius: 16px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
    margin-bottom: 24px;
    position: fixed;
    top: 0;
    left: 15%;
    right: 0;
    z-index: 1000;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .content {
    background: white;
    border-radius: 16px;
    padding: 24px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
    margin-top: 100px;
  }
  
  .header h2 {
    margin: 0;
    color: #1a237e;
    font-size: 24px;
    font-weight: 600;
  }
  
  .content {
    width: 90%;
    background: white;
    border-radius: 16px;
    padding: 24px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
    margin: 100px auto 0;
  }
  
  .search-bar {
    margin-bottom: 24px;
    display: flex;
    gap: 16px;
    align-items: center;
  }
  
  .el-input {
    flex: 1;
  }
  
  .el-input__inner {
    border-radius: 8px;
    border: 1px solid #e0e0e0;
    padding: 12px 16px;
    transition: all 0.3s ease;
  }
  
  .el-input__inner:focus {
    border-color: #3949ab;
    box-shadow: 0 0 0 2px rgba(57, 73, 171, 0.1);
  }
  
  :deep(.el-table) {
    border-radius: 16px;
    overflow: hidden;
  }
  
  :deep(.el-table__row) {
    border-radius: 8px;
    margin-bottom: 8px;
  }

  :deep(.el-table th) {
    background-color: #f5f7fa !important;
    color: #1a237e;
    font-weight: 600;
    padding: 16px;
  }

  :deep(.el-table td) {
    color: #546e7a;
    padding: 16px;
  }

  :deep(.el-table--striped .el-table__body tr.el-table__row--striped td) {
    background: #fafbfc;
  }
  
  .el-pagination {
    margin-top: 16px;
    justify-content: flex-end;
  }
  
  :deep(.el-button) {
    padding: 8px 16px;
    border-radius: 6px;
    font-weight: 500;
    transition: all 0.3s ease;
  }

  :deep(.el-button--primary) {
    background: linear-gradient(90deg, #3949ab 0%, #5c6bc0 100%);
    border: none;
  }

  :deep(.el-button--primary:hover) {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(57, 73, 171, 0.3);
  }

  :deep(.el-button--primary.active) {
    background: #1a237e;
    box-shadow: 0 2px 8px rgba(26, 35, 126, 0.3);
  }

  :deep(.el-button--danger) {
    background: linear-gradient(90deg, #c62828 0%, #d32f2f 100%);
    border: none;
  }

  :deep(.el-button--danger:hover) {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(198, 40, 40, 0.3);
  }
  
  .el-dialog {
    border-radius: 16px;
    overflow: hidden;
  }
  
  .el-dialog__header {
    background: #f5f7fa;
    padding: 20px 24px;
    margin: 0;
  }
  
  .el-dialog__title {
    color: #1a237e;
    font-size: 18px;
    font-weight: 600;
  }
  
  .el-dialog__body {
    padding: 24px;
  }
  
  .el-form-item__label {
    color: #546e7a;
    font-weight: 500;
  }
  
  .el-form-item__content {
    margin-top: 8px;
  }
  
  .room-status {
    padding: 6px 12px;
    border-radius: 20px;
    font-size: 14px;
    font-weight: 500;
  }
  
  .room-status.available {
    background-color: #e8f5e9;
    color: #2e7d32;
  }
  
  .room-status.occupied {
    background-color: #ffebee;
    color: #c62828;
  }
  
  .room-status.maintenance {
    background-color: #fff3e0;
    color: #ef6c00;
  }
  
  @media (max-width: 768px) {
    .management-container {
      padding: 16px;
      margin-left: 0;
    }

    .header {
      padding: 16px;
      margin-bottom: 16px;
      flex-direction: column;
      gap: 16px;
      top: 16px;
    }

    .content {
      padding: 16px;
    }

    :deep(.el-table th),
    :deep(.el-table td) {
      padding: 12px;
    }

    .action-buttons {
      flex-direction: column;
      align-items: stretch;
    }

    .action-buttons .el-button {
      width: 100%;
    }
  }
  </style>